package com.xiaoxin.nmzp.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.exception.file.InvalidExtensionException;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.system.domain.NmzpCompany;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.xiaoxin.nmzp.service.INmzpCompanyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 公司信息Controller
 *
 * @author xiaoxin
 * @date 2024-10-18
 */
@RestController
@RequestMapping("/nmzp/company")
public class NmzpCompanyController extends BaseController {
    @Autowired
    private INmzpCompanyService nmzpCompanyService;

    /**
     * 查询公司信息列表
     */
    @PreAuthorize("@ss.hasPermi('nmzp:company:list')")
    @GetMapping("/list")
    public TableDataInfo list(NmzpCompany nmzpCompany) {

        startPage();
        List<NmzpCompany> list = nmzpCompanyService.selectNmzpCompanyList(nmzpCompany);
        return getDataTable(list);
    }

    /**
     * 导出公司信息列表
     */
    @PreAuthorize("@ss.hasPermi('nmzp:company:export')")
    @Log(title = "公司信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NmzpCompany nmzpCompany) {
        List<NmzpCompany> list = nmzpCompanyService.selectNmzpCompanyList(nmzpCompany);
        ExcelUtil<NmzpCompany> util = new ExcelUtil<NmzpCompany>(NmzpCompany.class);
        util.exportExcel(response, list, "公司信息数据");
    }

    /**
     * 获取公司信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('nmzp:company:query')")
    @GetMapping(value = "/{companyId}")
    public AjaxResult getInfo(@PathVariable("companyId") Long companyId) {
        return success(nmzpCompanyService.selectNmzpCompanyByCompanyId(companyId));
    }

    /**
     * 新增公司信息
     */
    @PreAuthorize("@ss.hasPermi('nmzp:company:add')")
    @Log(title = "公司信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NmzpCompany nmzpCompany) {
        return toAjax(nmzpCompanyService.insertNmzpCompany(nmzpCompany));
    }

    /**
     * 修改公司信息
     */
    @PreAuthorize("@ss.hasPermi('nmzp:company:edit')")
    @Log(title = "公司信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NmzpCompany nmzpCompany) {
        return toAjax(nmzpCompanyService.updateNmzpCompany(nmzpCompany));
    }

    /**
     * 删除公司信息
     */
    @PreAuthorize("@ss.hasPermi('nmzp:company:remove')")
    @Log(title = "公司信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{companyIds}")
    public AjaxResult remove(@PathVariable Long[] companyIds) {
        return toAjax(nmzpCompanyService.deleteNmzpCompanyByCompanyIds(companyIds));
    }

    /**
     * 企业信息审核
     *
     * @param nmzpCompany
     * @return
     */
    @PostMapping("/examine")
    @ResponseBody
    public AjaxResult examine(@RequestBody NmzpCompany nmzpCompany) {
        return toAjax(nmzpCompanyService.examine(nmzpCompany));
    }

    /**
     * 公司信息修改
     *
     * @param nmzpCompany
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public AjaxResult update(@RequestBody NmzpCompany nmzpCompany) {
        return toAjax(nmzpCompanyService.updateNmzpCompany(nmzpCompany));
    }

    /**
     * 公司营业执照上传
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public ResponseEntity<String> uploadLicense(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return ResponseEntity.badRequest().body("文件为空");
        }
        try {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            System.out.println("文件路径："+avatar);
            if (avatar != null) {
                int update = nmzpCompanyService.update(avatar);
                return ResponseEntity.ok("文件上传成功: " + avatar);
            }

        } catch (IOException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件上传失败: " + e.getMessage());
        } catch (InvalidExtensionException e) {
            throw new RuntimeException(e);
        }
        return ResponseEntity.badRequest().body("文件格式不正确");
    }



}

